package com.sailfish.springbootdemo.service.db1;

import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.configs.UserHeaderHolder;
import com.sailfish.springbootdemo.dao.db1.MaintenanceConfigDao;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.MaintenanceConfig;
import com.sailfish.springbootdemo.pojo.db4.Operate;
import com.sailfish.springbootdemo.service.db4.OperateService;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MaintenanceConfigServiceImpl implements MaintenanceConfigService {

    @Autowired
    private MaintenanceConfigDao maintenanceConfigDao;

    @Autowired
    private OperateService operateService;

    private String operateSmallType = "参数配置";

    @Override
    public List<MaintenanceConfig> getMaintenanceConfigByType(Integer maintenanceConfigType) {
        return maintenanceConfigDao.getMaintenanceConfigByType(maintenanceConfigType);
    }

    @Override
    public Result updateMaintenanceConfig(MaintenanceConfig maintenanceConfig) {
        try {
            if (maintenanceConfig == null) {
                return ResultUtil.error(500, "缺少参数maintenanceConfig", "Miss Param maintenanceConfig", null);
            }
            if (maintenanceConfig.getMaintenanceConfigId() == null || "".equals(maintenanceConfig.getMaintenanceConfigId().toString())) {
                return ResultUtil.error(500, "缺少参数maintenanceConfigId", "Miss Param maintenanceConfigId", null);
            }

            String userId = UserHeaderHolder.getUserId();
            MaintenanceConfig oldMaintenanceConfig = maintenanceConfigDao.getMaintenanceConfigById(maintenanceConfig.getMaintenanceConfigId());

            maintenanceConfig.setUpdateTime(new Date());
            Integer integer = maintenanceConfigDao.updateMaintenanceConfig(maintenanceConfig);
            if (integer > 0) {
                // 操作记录
                operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 3, 1, new Date(), JSONObject.toJSONString(oldMaintenanceConfig), JSONObject.toJSONString(maintenanceConfig)));
                return ResultUtil.success(null);
            } else {
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result updateSendEmailUsers(Integer maintenanceConfigId, String sendEmailUsers) {
        try {
            Integer integer = maintenanceConfigDao.updateSendEmailUsers(maintenanceConfigId, sendEmailUsers);
            if (integer > 0) {
                // 操作记录
                return ResultUtil.success(null);
            } else {
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }
}
